package com.baizhi.controller;

import com.baizhi.clients.FileClient;
import com.baizhi.entity.Emp;
import com.baizhi.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

    @Autowired
    private FileClient fileClient;

    /*
    * 更新员工信息
    * */
    @PostMapping("/emp/update")
    public Map<String, Object> update(Emp emp,MultipartFile photo){
        Map<String, Object> map = new HashMap<>();
        try {
            //1.保存文件
            log.info("接收到的文件信息：" + photo.getOriginalFilename());
            Map<String, Object> uploadMap = fileClient.upload(photo);
            if (uploadMap.get("state").equals("false")) {
                throw new RuntimeException("提示：头像上传失败");
            }
            log.info("头像地址：[{}]", uploadMap.get("url").toString());
            emp.setPath(uploadMap.get("url").toString());

            //2.保存或更新员工
            empService.save(emp);
            map.put("msg", "提示：员工信息保存/更新成功!");
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", e.getMessage());
            map.put("state", false);
        }
        //3.响应数据
        return map;
    }

    /*
     * 查询员工详细信息
     * */
    @GetMapping("/emp/findOne")
    public Emp findOne(Integer id) {
        return empService.findOne(id);
    }

    /*
     * 删除员工信息
     * */
    @GetMapping("/emp/delete")
    public Map<String, Object> delete(Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            empService.delete(id);
            map.put("state", true);
            map.put("msg", "提示：删除员工信息成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    /*
     * 保存员工信息
     * */
    @PostMapping("/emp/save")
    public Map<String, Object> save(Emp emp, MultipartFile photo) {

        Map<String, Object> map = new HashMap<>();
        try {
            //1.保存文件
            log.info("接收到的文件信息：" + photo.getOriginalFilename());
            Map<String, Object> uploadMap = fileClient.upload(photo);
            if (uploadMap.get("state").equals("false")) {
                throw new RuntimeException("提示：头像上传失败");
            }
            log.info("头像地址：[{}]", uploadMap.get("url").toString());
            emp.setPath(uploadMap.get("url").toString());

            //2.保存员工
            empService.save(emp);
            map.put("msg", "提示：员工信息保存成功!");
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", e.getMessage());
            map.put("state", false);
        }

        //3.响应数据

        return map;
    }

    /*
     * 查询所有员工
     * */
    @GetMapping("/emp/findAll")
    public List<Emp> findAll() {
        return empService.findAll();
    }
}
